package com.start.data.screen.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.common.entity.StartBaseEntity;
import com.start.core.annotations.RegisterMenu;
import com.start.core.annotations.RegisterPermission;
import com.start.core.annotations.StartLog;
import com.start.data.screen.entity.DataScreenDynamicSql;
import com.start.data.screen.service.DataScreenDynamicSqlService;
import com.start.security.annotations.DataPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dataScreen/visval")
@RegisterMenu(value = "动态sql管理", systemCode = "start:data:screen:dynamicSql", component = "views/start/data/dynamicSql/index", path = "data/dynamicSql/index")
public class DataScreenDynamicSqlController {
    private static final String TITLE = "动态sql管理";

    @Autowired
    private DataScreenDynamicSqlService sysFormConfigService;


    @GetMapping("/page")
    @StartLog(value = TITLE, action = "列表查询")
    @DataPermission(value = "views/start/data/dynamicSql/index")
    public Result<?> page(Page<DataScreenDynamicSql> page, DataScreenDynamicSql entity) {
        QueryWrapper<DataScreenDynamicSql> lambdaQuery = DataQueryConstructor.createEntity(entity);
        LambdaQueryWrapper<DataScreenDynamicSql> lambda = lambdaQuery.lambda();
        lambda.orderByDesc(StartBaseEntity::getCreateTime);
        return Result.ok(sysFormConfigService.page(page, lambda));
    }

    @PostMapping("/add")
    @StartLog(value = TITLE, action = "添加")
    @PreAuthorize("@pas.hasPermission('start:data:screen:dynamicSql:add')")
    @RegisterPermission(value = "start:data:screen:dynamicSql:add", name = "添加")
    public Result<?> add(@RequestBody DataScreenDynamicSql entity) {
        return Result.ok(entity.getId());
    }

    @PutMapping("/updateById")
    @StartLog(value = TITLE, action = "修改")
    @PreAuthorize("@pas.hasPermission('start:data:screen:dynamicSql:edit')")
    @RegisterPermission(value = "start:data:screen:dynamicSql:edit", name = "修改")
    public Result<?> updateById(@RequestBody DataScreenDynamicSql entity) {
        sysFormConfigService.updateById(entity);
        return Result.ok();
    }


    @GetMapping("/getById/{id}")
    @StartLog(value = TITLE, action = "查看")
    @PreAuthorize("@pas.hasPermission('start:data:screen:dynamicSql:info')")
    @RegisterPermission(value = "start:data:screen:dynamicSql:info", name = "查看")
    public Result<?> getById(@PathVariable("id") String id) {
        return Result.ok(sysFormConfigService.getById(id));
    }


    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@pas.hasPermission('start:data:screen:dynamicSql:delete')")
    @RegisterPermission(value = "start:data:screen:dynamicSql:delete", name = "删除")
    public Result<?> deleteById(@PathVariable("id") String id) {
        sysFormConfigService.removeById(id);
        return Result.ok();
    }


}
